/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZMutableEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_22033
/*     */   extends UpgradeOp
/*     */ {
/*     */   private Type mType;
/*     */   
/*     */   static enum Type
/*     */   {
/*  38 */     account, 
/*  39 */     alias, 
/*  40 */     calendarresource, 
/*  41 */     config, 
/*  42 */     cos, 
/*  43 */     datasource, 
/*  44 */     distributionlist, 
/*  45 */     domain, 
/*  46 */     identity, 
/*  47 */     server, 
/*  48 */     signature, 
/*  49 */     xmppcomponent, 
/*  50 */     zimlet;
/*     */     
/*     */     private Type() {}
/*     */     
/*  54 */     public static Type fromString(String s) throws ServiceException { try { return valueOf(s);
/*     */       } catch (IllegalArgumentException e) {
/*  56 */         throw ServiceException.INVALID_REQUEST("unknown type: " + s, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   boolean parseCommandLine(CommandLine cl)
/*     */   {
/*  65 */     String[] args = cl.getArgs();
/*  66 */     if (args.length == 1) {
/*     */       try {
/*  68 */         this.mType = Type.fromString(args[0]);
/*     */       } catch (ServiceException e) {
/*  70 */         LdapUpgrade.usage(null, this, "invalid type: " + args[0]);
/*  71 */         return false;
/*     */       }
/*  73 */     } else if (args.length != 0) {
/*  74 */       LdapUpgrade.usage(null, this, "invalid arg");
/*  75 */       return false;
/*     */     }
/*  77 */     return true;
/*     */   }
/*     */   
/*     */   void usage(HelpFormatter helpFormatter)
/*     */   {
/*  82 */     this.printer.println();
/*  83 */     this.printer.println("args for bug " + this.bug + ":");
/*  84 */     this.printer.println("    [type]");
/*  85 */     this.printer.println();
/*  86 */     this.printer.println("type can be: (if omitted, it means all objects)");
/*  87 */     for (Type t : Type.values()) {
/*  88 */       this.printer.println("    " + t.name());
/*     */     }
/*  90 */     this.printer.println();
/*     */   }
/*     */   
/*     */   private static class Bug22033Visitor extends SearchLdapOptions.SearchLdapVisitor
/*     */   {
/*     */     private UpgradeOp upgradeOp;
/*     */     private ZLdapContext modZlc;
/*  97 */     private int numModified = 0;
/*     */     
/*     */     private Bug22033Visitor(UpgradeOp upgradeOp, ZLdapContext modZlc) {
/* 100 */       super();
/* 101 */       this.upgradeOp = upgradeOp;
/* 102 */       this.modZlc = modZlc;
/*     */     }
/*     */     
/*     */     public void visit(String dn, IAttributes ldapAttrs)
/*     */     {
/*     */       try {
/* 108 */         doVisit(dn, (ZAttributes)ldapAttrs);
/*     */       } catch (ServiceException e) {
/* 110 */         this.upgradeOp.printer.println("entry skipped, encountered error while processing entry at:" + dn);
/* 111 */         this.upgradeOp.printer.printStackTrace(e);
/*     */       }
/*     */     }
/*     */     
/*     */     private void doVisit(String dn, ZAttributes ldapAttrs) throws ServiceException
/*     */     {
/* 117 */       String createTime = ldapAttrs.getAttrString("createTimestamp");
/*     */       
/* 119 */       ZMutableEntry entry = LdapClient.createMutableEntry();
/* 120 */       entry.setAttr("zimbraCreateTimestamp", createTime);
/* 121 */       this.upgradeOp.replaceAttrs(this.modZlc, dn, entry);
/*     */       
/* 123 */       this.numModified += 1;
/*     */     }
/*     */     
/*     */     private int getNumModified() {
/* 127 */       return this.numModified;
/*     */     }
/*     */   }
/*     */   
/*     */   void doUpgrade() throws ServiceException
/*     */   {
/* 133 */     LdapDIT dit = this.prov.getDIT();
/*     */     
/*     */ 
/* 136 */     String[] returnAttrs = { "objectClass", "zimbraCreateTimestamp", "createTimestamp" };
/*     */     
/*     */     String query;
/*     */     String base;
/* 140 */     if (this.mType == null) {
/* 141 */       this.printer.println("Checking all objects\n");
/*     */       
/* 143 */       String base = dit.zimbraBaseDN();
/* 144 */       query = "(|(objectclass=zimbraAccount)(objectclass=zimbraAlias)(objectclass=zimbraCalendarResource)(objectclass=zimbraGlobalConfig)(objectclass=zimbraCOS)(objectclass=zimbraDataSource)(objectclass=zimbraDistributionList)(objectclass=zimbraDomain)(objectclass=zimbraIdentity)(objectclass=zimbraServer)(objectclass=zimbraSignature)(objectclass=zimbraXMPPComponent)(objectclass=zimbraZimletEntry))";
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/*     */     else
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 160 */       this.printer.println("Checking " + this.mType.name() + " objects...\n");
/*     */       
/* 162 */       switch (this.mType) {
/*     */       case account: 
/* 164 */         base = dit.mailBranchBaseDN();
/* 165 */         query = "(&(objectclass=zimbraAccount)(!(objectclass=zimbraCalendarResource)))";
/* 166 */         break;
/*     */       case alias: 
/* 168 */         base = dit.mailBranchBaseDN();
/* 169 */         query = "(objectclass=zimbraAlias)";
/* 170 */         break;
/*     */       case calendarresource: 
/* 172 */         base = dit.mailBranchBaseDN();
/* 173 */         query = "(objectclass=zimbraCalendarResource)";
/* 174 */         break;
/*     */       case config: 
/* 176 */         base = dit.configDN();
/* 177 */         query = "(objectclass=zimbraGlobalConfig)";
/* 178 */         break;
/*     */       case cos: 
/* 180 */         base = dit.cosBaseDN();
/* 181 */         query = "(objectclass=zimbraCOS)";
/* 182 */         break;
/*     */       case datasource: 
/* 184 */         base = dit.mailBranchBaseDN();
/* 185 */         query = "(objectclass=zimbraDataSource)";
/* 186 */         break;
/*     */       case distributionlist: 
/* 188 */         base = dit.mailBranchBaseDN();
/* 189 */         query = "(objectclass=zimbraDistributionList)";
/* 190 */         break;
/*     */       case domain: 
/* 192 */         base = dit.domainBaseDN();
/* 193 */         query = "(objectclass=zimbraDomain)";
/* 194 */         break;
/*     */       case identity: 
/* 196 */         base = dit.mailBranchBaseDN();
/* 197 */         query = "(objectclass=zimbraIdentity)";
/* 198 */         break;
/*     */       case server: 
/* 200 */         base = dit.serverBaseDN();
/* 201 */         query = "(objectclass=zimbraServer)";
/* 202 */         break;
/*     */       case signature: 
/* 204 */         base = dit.mailBranchBaseDN();
/* 205 */         query = "(objectclass=zimbraSignature)";
/* 206 */         break;
/*     */       case xmppcomponent: 
/* 208 */         base = dit.xmppcomponentBaseDN();
/* 209 */         query = "(objectclass=zimbraXMPPComponent)";
/* 210 */         break;
/*     */       case zimlet: 
/* 212 */         base = dit.zimletBaseDN();
/* 213 */         query = "(objectclass=zimbraZimletEntry)";
/* 214 */         break;
/*     */       default: 
/* 216 */         throw ServiceException.FAILURE("", null);
/*     */       }
/*     */       
/*     */     }
/* 220 */     String query = "(&(!(zimbraCreateTimestamp=*))" + query + ")";
/*     */     
/* 222 */     ZLdapContext zlc = null;
/* 223 */     ZLdapContext modZlc = null;
/* 224 */     Bug22033Visitor visitor = null;
/*     */     try
/*     */     {
/* 227 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/* 228 */       modZlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */       
/* 230 */       visitor = new Bug22033Visitor(this, modZlc, null);
/*     */       
/* 232 */       SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), returnAttrs, 0, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
/*     */       
/*     */ 
/*     */ 
/* 236 */       zlc.searchPaged(searchOpts);
/*     */     }
/*     */     catch (ServiceException e) {
/* 239 */       throw ServiceException.FAILURE("unable to list all objects", e);
/*     */     } finally {
/* 241 */       LdapClient.closeContext(zlc);
/* 242 */       LdapClient.closeContext(modZlc);
/*     */       
/* 244 */       if (visitor != null) {
/* 245 */         this.printer.println("\nModified " + visitor.getNumModified() + " objects");
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_22033.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */